/**
 * @file 设备列表插件
 */
import treeMenu from '../Menu/menu.vue';
import $ from 'jquery';
export default {
    components: {
        treeMenu
    },
    props: {
        selectedChange: {
            type: Function,
            default: () => { }
        },
        selectedMode: {
            type: Number,
            default: 0
        },
        equipSelected: {
            type: Array,
            default: () => []
        },
        showSetParm: {
            type: Boolean,
            default: false
        }
    },
    data () {
        return {
            equipLoading: false, // 设备加载loading
            equipList: [], // 设备列表
            searchName: '', // 搜索框
            selectedList: [],
            total: 10,
            currentPage: 1,
            pageSize: 5,
            dialogVisible: true,
            noData: '暂无数据',
            equipNo: -1,
            setParmList: [],
            setParmCurrentPage: 1,
            setParmPageSize: 8,
            setParmTotal: 0,
            setParmCurrentIndex: -1,
            setParmData: {
                setNo: -1,
                setName: ''
            }
        };
    },
    mounted () {

        // 缓存 ，搜索用API请求
        // this.equipLoading = true;
        // if (this.globalVariable.initsEquipAList.groups && !this.globalVariable.search) {
        //     setTimeout(() => {
        //         this.equipLoading = false;
        //         this.equipList = this.globalVariable.initsEquipAList.groups;
        //     }, 500);
        // } else {
        //     this.searchEquips();
        // }
        this.searchEquips();

        if (this.showSetParm) {
            $('.menuDialog .equipSelect').addClass('showSetParm');
        }
    },
    methods: {
        setParmHandleChange (val) {
            this.setParmCurrentPage = val;
            this.getItem();
        },
        getItem (item) {

            // 当需要选择设备控制项的时候传equipNo
            if (item) {
                this.equipNo = item.equipNo;
            }
            this.$api
                .getSetParm({
                    pageNo: this.setParmCurrentPage,
                    pageSize: this.setParmPageSize,
                    equipNo: this.equipNo
                })
                .then((rt) => {
                    let code = rt.data.code;
                    if (code !== 200) {
                        let msg = rt.data.message;
                        this.$message({
                            type: 'error',
                            message: msg
                        });
                    } else {
                        this.setParmTotal = rt.data.data.totalCount;
                        this.setParmList = JSON.parse(rt.data.data.list);
                    }
                });
        },
        selectedSetParm (item) {
            this.setParmCurrentIndex = item.setNo;
            this.setParmData.setNo = item.setNo;
            this.setParmData.setName = item.setNm;
        },
        sendData () {
            let equipNoList = [];
            if (this.selectedMode === 0) {
                let dataList = $('.child_tags');
                dataList.each((item) => {
                    if (
                        $($('.child_tags')[item])
                            .children('label')
                            .hasClass('checkInput-active')
                    ) {
                        if (
                            $($('.child_tags')[item])
                                .children('label')
                                .attr('no') !== 0
                        ) {
                            equipNoList.push({
                                equipNo: parseInt(
                                    $($('.child_tags')[item])
                                        .children('label')
                                        .attr('no'),
                                    10
                                ),
                                equipName: $($('.child_tags')[item])
                                    .children('a')
                                    .text()
                            });
                        }
                    }
                });
            } else if ($('.selectedColor')) {
                if ($('.selectedColor').length > 0) {
                    equipNoList.push({
                        equipName: $('.selectedColor')
                            .children('a')
                            .text().trim(),
                        equipNo: parseInt($('.selectedColor').attr('equipno'), 10)
                    });
                }
            }
            if (equipNoList.length === 0) {
                this.$message({
                    message: '请选择设备',
                    type: 'warning'
                });
            } else if (this.showSetParm && this.setParmData.setNo === -1) {
                this.$message({
                    message: '请选择设备控制项',
                    type: 'warning'
                });
            } else {
                this.filterData(equipNoList);
            }
        },
        filterData (data) {
            let Arr = [];
            let ArrList = [];
            data.forEach((item) => {
                if (Arr.indexOf(item.equipNo) === -1) {
                    Arr.push(item.equipNo);
                    ArrList.push(item);
                }
            });
            if (this.showSetParm) {
                this.selectedChange(ArrList, this.setParmData);
            } else {
                this.selectedChange(ArrList);
            }
        },
        handleCurrentChange (val) {
            this.currentPage = val;
            this.searchEquips();
        },
        onSearch () {
            this.searchEquips();
            this.globalVariable.search = true;
        },

        // 获取左侧设备列表
        searchEquips () {
            let data = {
                pageNo: this.currentPage,
                pageSize: this.pageSize,
                SearchName: this.searchName
            };
            this.equipLoading = true;
            this.$api
                .GroupListNoState(data)
                .then((rt) => {

                    // 搜索请求结果
                    if (rt.data.message === '无查找结果') {
                        this.equipList = [];
                        return;
                    }
                    if (rt.data.code === 200) {
                        this.equipList = rt.data.data.groups;
                        this.total = rt.data.data.groups.length;
                        // if (!this.globalVariable.search) {
                        //     this.globalVariable.initsEquipAList = rt.data.data;
                        // }
                        if (this.searchName.length === 0) {
                            this.globalVariable.search = false;
                        }
                    }
                    this.equipLoading = false;
                })
                .catch((err) => {
                    this.equipLoading = false;
                    console.log('err---', err);
                });

        }
    }
};